package com.集合.Collection;
/*Collection集合存储学生对象并遍历*/
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;
public class CollectionDemo {
    public static void main(String[] args){
        //创建Collection集合对象
        Collection<Student> c = new ArrayList<Student>();

        //创建学生对象
        Student s1 = new Student("小李",19);
        Student s2 = new Student("张三",29);
        Student s3 = new Student("李四",39);

        //把学生添加到集合
        c.add(s1);
        c.add(s2);
        c.add(s3);

        //迭代器遍历集合
        Iterator<Student> it = c.iterator();
//        while(it.hasNext()){
//            System.out.println(it.next());
//        }

        //除了重写toString()方法，还可以改为以下程序
        //不过重写toString()方法可以自定义输出格式。
        while(it.hasNext()){
            Student s = it.next();
            System.out.println(s.getName()+","+s.getAge());
        }
    }
}
